package com.xhldtc.proxy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

public class Transfer implements Runnable
{
	private Socket socket;

	public Transfer(Socket socket)
	{
		this.socket = socket;
	}

	public void run()
	{
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try
		{
//			URL url = new URL("http://localhost:8080/proxy/main");
//			URLConnection conn = url.openConnection();
//			conn.setDoOutput(true);
//			conn.setDoInput(true);

			bis = new BufferedInputStream(socket.getInputStream());
//			bos = new BufferedOutputStream(conn.getOutputStream());

			byte[] buffer = new byte[1024];
			int n;

			StringBuilder sb = new StringBuilder();

			while ((n = bis.read(buffer, 0, 1024)) != -1)
			{
				// bos.write(buffer, 0, n);
				sb.append(new String(buffer, 0, n));
//				System.out.println(sb);
			}
			System.out.println(sb);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				bis.close();
				bos.close();
				socket.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	private void requestProxy(Socket socket, OutputStream os)
	{
		try
		{
			BufferedInputStream bis = new BufferedInputStream(
					socket.getInputStream());
			BufferedOutputStream bos = new BufferedOutputStream(os);

			byte[] buffer = new byte[1024];
			int n;

			while ((n = bis.read(buffer, 0, 1024)) != -1)
			{
				bos.write(buffer, 0, n);
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
